﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BL;
using Entities;

namespace WindowsFormsApplication1
{
    public partial class frmDonHangMoi : Form
    {
        DonHangMoiBL donHangMoiBL;
        BindingSource dhm = new BindingSource();        
        int donHangMoi;

        DataTable dtDonHang = new DataTable();
        DonHangBL donHangBL;

        KhuonBL khuonBL;
        BindingSource bskhuon = new BindingSource();
        DataTable dtKhuon = null;
        DataSet dsKhuon;

        NapChupBL napChupBL;
        BindingSource bsnapchup = new BindingSource();
        DataTable dtNapChup = null;
        DataSet dsNapChup;

        RapBL rapBL;
        BindingSource bsrap = new BindingSource();
        DataTable dtRap = null;
        DataSet dsRap;

        ThongTinDonHangBL thongTinDonHangBL;
        DataTable dtChonDonHang;
        DataSet dsChonDonHang;

        TheoDoiCongNhanBL theoDoiCongNhanBL;
        BindingSource themCN = new BindingSource();
        int themCongNhan;

        CongNhanBL congNhanBL;
        DataTable dtCongNhan;


        public frmDonHangMoi()
        {
            InitializeComponent();
            donHangMoiBL = new DonHangMoiBL();
            donHangBL = new DonHangBL();
            khuonBL = new KhuonBL();
            napChupBL = new NapChupBL();
            rapBL = new RapBL();
            thongTinDonHangBL = new ThongTinDonHangBL();
            theoDoiCongNhanBL = new TheoDoiCongNhanBL();
            congNhanBL = new CongNhanBL();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            try
            {
                dtChonDonHang = thongTinDonHangBL.chonDonHang().Tables[0];
                foreach (DataRow item in dtChonDonHang.Rows)
                {
                    cmbMaDonHang.Items.Add(item[0].ToString());
                }
                cmbMaDonHang.SelectedIndex = 0;

                dtCongNhan = congNhanBL.layDanhSachMaVaTenCongNhan().Tables[0];

                //foreach (DataRow item in dtCongNhan.Rows)
                //{
                //    cmbMaCongNhan.Items.Add(item[0].ToString());    
                //}
                cmbMaCongNhan.DataSource = dtCongNhan;
                cmbMaCongNhan.DisplayMember = "TenCongNhan";
                cmbMaCongNhan.ValueMember = "MaCongNhan";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //new KhachHang().Show();
            //this.Hide();
        }

        private void button6_Click(object sender, EventArgs e)
        {
           // new DonHang().Show();
           // this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
          //  new SanPham().Show();
            // this.Hide();
        }

        private void button4_Click(object sender, EventArgs e)
        {
           // new CongNhan().Show();
            // this.Hide();
        }

        private void button5_Click(object sender, EventArgs e)
        {
           // new CongCu().Show();
            // this.Hide();
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void textBox35_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnTimKiemNapChup_Click(object sender, EventArgs e)
        {
            try
            {
                dgvNapChup.AutoGenerateColumns = false;

                dsNapChup = napChupBL.timKiemNapChup(txtMaNapChup.Text);
                bsnapchup.DataSource = dsNapChup.Tables[0];
                dgvNapChup.DataSource = bsnapchup;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }

        private void dataGridView7_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }


        private void btnTaoMoi_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    donHangMoi = donHangMoiBL.taoDonHangMoi(txtMaDonHang.Text, txtMaKhachHang.Text, txtDiaChiGiaoHang.Text, txtNgayNhan.Text, txtNgayGiao.Text);
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}

            
        }

        private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            //try
            //{
            //    dhm.EndEdit();
            //    dgvDonHangMoi.EndEdit();
            //    donHangMoiBL.updateDonHangMoi(dtDonHang);
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}


        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            try
            {
                dgvBanThanhPham.DataSource = donHangBL.sanXuatBanThanhPham(txtXem.Text).Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

          
        }

        private void btnTimKiemKhuon_Click(object sender, EventArgs e)
        {
            try
            {
                dgvKhuon.AutoGenerateColumns = false;

                dsKhuon = khuonBL.timKiemKhuon(txtMaKhuon.Text);
                bskhuon.DataSource = dsKhuon.Tables[0];
                dgvKhuon.DataSource = bskhuon;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }

        private void dgvKhuon_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongSanXuat() == true)
                {
                    dgvKhuon.AutoGenerateColumns = false;
                    bskhuon.EndEdit();
                    dgvKhuon.EndEdit();
                    khuonBL.updateKhuon(dsKhuon.Tables[0]);
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgvNapChup_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongSanXuat() == true)
                {
                    dgvNapChup.AutoGenerateColumns = false;
                    bsnapchup.EndEdit();
                    dgvNapChup.EndEdit();
                    napChupBL.updateNapChup(dsNapChup.Tables[0]);
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnTimKiemRap_Click(object sender, EventArgs e)
        {
            try
            {
                    dgvRap.AutoGenerateColumns = false;
                    dsRap = rapBL.timKiemRap(txtRap.Text);
                    bsrap.DataSource = dsRap.Tables[0];
                    dgvRap.DataSource = bsrap;                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void dgvRap_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongSanXuat() == true)
                {
                    bsrap.EndEdit();
                    dgvRap.EndEdit();
                    rapBL.updateRap(dsRap.Tables[0]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
        
        private void cmbMaDonHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                cmbMaSanPham.Items.Clear();
                DataTable dtSanPhamTheoDonHang = donHangBL.laySanPhamTheoMaDonHang(cmbMaDonHang.SelectedItem.ToString()).Tables[0];
                foreach (DataRow item in dtSanPhamTheoDonHang.Rows)
                {
                    cmbMaSanPham.Items.Add(item[0].ToString());
                }
                cmbMaSanPham.SelectedIndex = 0;

                dgvCongNhanDH.DataSource = theoDoiCongNhanBL.layDanhSachTheoDoiCongNhan(cmbMaDonHang.SelectedItem.ToString()).Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

           
        }

        private void btnThemVaoDonHang_Click(object sender, EventArgs e)
        {
            try
            {
                if (QuanLyTaiKhoan.taiKhoanONhomXemThemXoaSuaPhongSanXuat() == true)
                {
                    themCongNhan = theoDoiCongNhanBL.themCongNhan(cmbMaCongNhan.SelectedValue.ToString(), cmbMaDonHang.SelectedItem.ToString(), cmbMaSanPham.Text);
                    dgvCongNhanDH.DataSource = theoDoiCongNhanBL.layDanhSachTheoDoiCongNhan(cmbMaDonHang.SelectedItem.ToString()).Tables[0];
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }         
        
        }

        private void btnDanhSachKhuon_Click(object sender, EventArgs e)
        {
            try
            {
                dgvKhuon.AutoGenerateColumns = false;
                dsKhuon = khuonBL.layDanhSachKhuon();
                bskhuon.DataSource = dsKhuon.Tables[0];
                dgvKhuon.DataSource = bskhuon;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }            
        }

        private void btnDanhSachNapChup_Click(object sender, EventArgs e)
        {
            try
            {
                dgvNapChup.AutoGenerateColumns = false;
                dsNapChup = napChupBL.layDanhSachNapChup();
                bsnapchup.DataSource = dsNapChup.Tables[0];
                dgvNapChup.DataSource = bsnapchup;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }
    }
}
